<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="保养单号" prop="recordCode">
        <el-input
          v-model="queryParams.recordCode"
          placeholder="请输入保养单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保养状态" prop="maintenanceStatus">
        <el-select v-model="queryParams.maintenanceStatus" placeholder="请选择保养状态" clearable size="small">
          <el-option label="未开始" value="0" />
          <el-option label="进行中" value="1" />
          <el-option label="已完成" value="2" />
          <el-option label="已取消" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="保养结果" prop="maintenanceResult">
        <el-select v-model="queryParams.maintenanceResult" placeholder="请选择保养结果" clearable size="small">
          <el-option label="正常" value="0" />
          <el-option label="异常" value="1" />
          <el-option label="待处理" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="保养日期">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sb:maintenance-record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBatchDelete"
          v-hasPermi="['sb:maintenance-record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sb:maintenance-record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="recordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="保养单号" align="center" prop="recordCode" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" :show-overflow-tooltip="true" />
      <el-table-column label="保养日期" align="center" prop="maintenanceDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保养人员" align="center" prop="maintainerName" />
      <el-table-column label="负责部门" align="center" prop="deptName" />
      <el-table-column label="保养状态" align="center" prop="maintenanceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.maintenance_status" :value="scope.row.maintenanceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="保养结果" align="center" prop="maintenanceResult">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.maintenance_result" :value="scope.row.maintenanceResult"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['sb:maintenance-record:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sb:maintenance-record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="handleStartMaintenance(scope.row)"
            v-hasPermi="['sb:maintenance-record:edit']"
            v-if="scope.row.maintenanceStatus === '0'"
          >开始保养</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sb:maintenance-record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改保养单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="保养单号" prop="recordCode">
              <el-input v-model="form.recordCode" placeholder="请输入保养单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称" prop="equipmentName">
              <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保养日期" prop="maintenanceDate">
              <el-date-picker clearable size="small" style="width: 200px"
                v-model="form.maintenanceDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择保养日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保养人员" prop="maintainerName">
              <el-input v-model="form.maintainerName" placeholder="请输入保养人员" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="负责部门" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入负责部门" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保养状态" prop="maintenanceStatus">
              <el-select v-model="form.maintenanceStatus" placeholder="请选择保养状态">
                <el-option label="未开始" value="0" />
                <el-option label="进行中" value="1" />
                <el-option label="已完成" value="2" />
                <el-option label="已取消" value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="保养结果" prop="maintenanceResult">
              <el-select v-model="form.maintenanceResult" placeholder="请选择保养结果">
                <el-option label="正常" value="0" />
                <el-option label="异常" value="1" />
                <el-option label="待处理" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际耗时" prop="actualDuration">
              <el-input-number v-model="form.actualDuration" :min="0" placeholder="请输入实际耗时（分钟）" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaintenanceRecord, getMaintenanceRecord, delMaintenanceRecord, addMaintenanceRecord, updateMaintenanceRecord, startMaintenance } from "@/api/sb/maintenanceRecord";

export default {
  name: "MaintenanceRecord",
  dicts: ['maintenance_status', 'maintenance_result'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保养单表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordCode: null,
        equipmentName: null,
        maintenanceStatus: null,
        maintenanceResult: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        recordCode: [
          { required: true, message: "保养单号不能为空", trigger: "blur" }
        ],
        equipmentName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        maintenanceDate: [
          { required: true, message: "保养日期不能为空", trigger: "blur" }
        ],
        maintainerName: [
          { required: true, message: "保养人员不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询保养单列表 */
    getList() {
      this.loading = true;
      listMaintenanceRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        recordCode: null,
        planId: null,
        equipmentId: null,
        equipmentName: null,
        maintenanceDate: null,
        maintainerId: null,
        maintainerName: null,
        deptId: null,
        deptName: null,
        maintenanceStatus: "0",
        maintenanceResult: "2",
        startTime: null,
        endTime: null,
        actualDuration: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加保养单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getMaintenanceRecord(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保养单";
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getMaintenanceRecord(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看保养单";
      });
    },
    /** 开始保养按钮操作 */
    handleStartMaintenance(row) {
      const recordId = row.recordId;
      this.$modal.confirm('确认开始执行保养吗？').then(function() {
        return startMaintenance(recordId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("开始执行保养成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateMaintenanceRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaintenanceRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除保养单编号为"' + row.recordCode + '"的数据项？').then(function() {
        return delMaintenanceRecord(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 批量删除按钮操作 */
    handleBatchDelete() {
      this.$modal.confirm('是否确认删除保养单编号为"' + this.ids + '"的数据项？').then(function() {
        return delMaintenanceRecord(this.ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sb/maintenance/record/export', {
        ...this.queryParams
      }, `maintenance_record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script> 